<?php
declare(strict_types=1);

namespace WebApp\lib;

/**
 * Class HttpHelper
 * @package WebApp\lib
 */
class HttpHelper
{
    private static $self;
    private function __construct()
    {
    }

    /**
     * @return HttpHelper
     */
    public static function getInstance()
    {
        if (!self::$self) {
            self::$self = new self;
        }
        return self::$self;
    }

    /**
     * @param $url
     * @param null $data
     * @param null $agent
     * @return mixed
     */
    public static function curlPost($url, $data = null, $agent = null)
    {
        $m_ch = curl_init();
        curl_setopt($m_ch, CURLOPT_URL, $url);
        curl_setopt($m_ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($m_ch, CURLOPT_SSL_VERIFYHOST, 0);
        if ($agent) {
            curl_setopt($m_ch, CURLOPT_USERAGENT, $agent);
        }

        curl_setopt($m_ch, CURLOPT_RETURNTRANSFER, 1);

        if ($data) {
            curl_setopt($m_ch, CURLOPT_POST, 1);
            curl_setopt($m_ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($m_ch, CURLOPT_TIMEOUT, 3);

        $output = curl_exec($m_ch);
        curl_close($m_ch);
        return $output;
    }


    /**
     * @param $url
     * @return resource
     */
    private function initCurl($url)
    {
        $m_ch = curl_init();
        curl_setopt($m_ch, CURLOPT_URL, $url);
        curl_setopt($m_ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($m_ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($m_ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($m_ch, CURLOPT_RETURNTRANSFER, 1);
        return $m_ch;
    }

    /**
     * @param $url
     * @return resource
     */
    private function initSSLCurl($url)
    {
        $m_ch = curl_init();
        curl_setopt($m_ch, CURLOPT_URL, $url);
        curl_setopt($m_ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($m_ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($m_ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($m_ch, CURLOPT_RETURNTRANSFER, 1);
        return $m_ch;
    }

    /**
     * @param $m_ch
     */
    private function setCurl($m_ch)
    {
        curl_setopt($m_ch, CURLOPT_HEADER, 0); //不返回header部分
    }

    /**
     * @param $url
     * @param array $data
     * @param string $cookie
     * @param array $setArr
     * @return mixed
     * @throws \ErrorException
     */
    public function httpCurl($url, $data = [], $cookie = '', $setArr = [])
    {
        if (strpos($url, "http://") === 0) {
            $m_ch = $this->initCurl($url);
        } elseif (strpos($url, "https://") === 0) {
            $m_ch = $this->initSSLCurl($url);
        } else {
            $m_ch = null;
            throwError("internal error");
        }

        if ($data) {
            curl_setopt($m_ch, CURLOPT_POST, true);
            curl_setopt($m_ch, CURLOPT_POSTFIELDS, $data);
        }

        $this->setCurl($m_ch);

        $setting_arr = [];
        if (!empty($cookie)) {
            $setting_arr[] = "X-Requested-With: XMLHttpRequest";
            $setting_arr[] = "Cookie:PHPSESSID={$cookie}";
        }

        if ($setArr) {
            array_merge($setting_arr, $setArr);
        }

        if ($setting_arr) {
            curl_setopt($m_ch, CURLOPT_HTTPHEADER, $setting_arr);
        }

        $o = curl_exec($m_ch);
        curl_close($m_ch);
        return $o;
    }
}
